/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.events.BasicEvent;

import fever.app.context.abstract.ContextContainer;
import fever.app.context.abstract.Dialog;
import fever.app.context.abstract.FeverContext;
import fever.app.context.abstract.FileSystem;
import fever.app.context.abstract.IContextFactory;
import fever.app.context.swfstudio.StudioContainer;
import fever.app.context.swfstudio.StudioDialog;
import fever.app.context.swfstudio.StudioFileSystem;
import fever.app.local.Localisation;
import fever.events.FeverController;
import fever.events.FeverEventList;
import fever.Fever;

/**
 * Builds a <strong>SWF Studio V3 context</strong> for application.
 * 
 * @see fever.app.context.ContextManager
 * 
 * @author Romain Ecarnot
 */
class fever.app.context.swfstudio.StudioContext extends FeverContext implements IContextFactory
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _instance : StudioContext;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Returns {@link fever.app.context.ContextManager} instance.
	 * 
	 * <p>Use {@link #init} method to define context.
	 */
	public static function getInstance( ) : StudioContext
	{
		if( !_instance ) _instance = new StudioContext( );
		return _instance;
	}
	
	/**
	 * Creates SWF Studio application container.
	 */
	public function createContainer() : ContextContainer
	{
		return new StudioContainer();
	}
	
	/**
	 * Inits Haxe FileSystem I/O.
	 */
	public function createFileSystem() : FileSystem
	{
		return new StudioFileSystem();
	}
	
	/**
	 * Creates dialog system.
	 */
	public function createDialog() : Dialog
	{
		return new StudioDialog();
	}
	
	/**
	 * Opens a external document.
	 */
	public function openDocument( path : String ) : Void
	{
		var return_obj = ssCore.Shell.invoke( { path : 'startdir://' + path } );
	}
	
	/** 
	 * Run an external application.
	 */
 	public function runProcess( app : String, args : String, workingDir : String, hidden : Boolean, waitFor : Boolean )
 	{
 		workingDir = ( workingDir ) ? workingDir : 'startdir://';
 		
 		var return_obj = ssCore.Shell.execute(
 			{
 				 path : app, 
 				 arguments : args, 
 				 currentDir : workingDir, 
 				 waitForExit : ( waitFor ) ? 'true' : 'false', 
 				 winState : ( hidden ) ? 'hidden' : 'normal',
 				 saveStdOut : ( waitFor ) ? 'true' : 'false'
 			}
		);
		
 		return return_obj.exitCode; 
 	}
 	
	/** 
	 * Quit application.
	 */
	public function quit( force : Boolean ) : Void
	{
		if( force == true ) ssCore.App.forceQuit();
		else
		{
			var o : FeverController = FeverController.getInstance();
			if( o.isRegistred( FeverEventList.onExitApplicationEVENT ) )
			{
				FeverController.getInstance().broadcastEvent( new BasicEvent( FeverEventList.onExitApplicationEVENT ) );
			}
			else
			{
				var r : Object = ssCore.App.showMsgBox(
					{ 
						icon : "question", 
						buttons : "YesNo", 
						defaultButton : "button2", 
						prompt : Localisation.getResource( 'fever:common.quitMessage' ), 
						title : Fever.application.getTitle()
					}
				);
				
				if( r.result == "YES" )	ssCore.App.forceQuit();
			}
		}
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
		
	/**
	 * Constructor.
	 */
	private function StudioContext()
	{
		super();
		
		ssDefaults.synchronousCommands = true;
	}

}